package cn.kgc.controller;

import cn.kgc.annotation.LoginType;
import cn.kgc.annotation.RequireLogin;
import cn.kgc.entity.Resume;
import cn.kgc.entity.ResumeProjectExp;
import cn.kgc.entity.ResumeSearchForm;
import cn.kgc.excpetion.GraceException;
import cn.kgc.form.ResumeUpdateForm;
import cn.kgc.form.ResumeWorkExpAddOrUpdateForm;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.grace.result.ResponseStatusEnum;
import cn.kgc.interceptor.JwtInterceptor;
import cn.kgc.service.ResumeProjectExpService;
import cn.kgc.service.ResumeService;
import cn.kgc.service.ResumeTccService;
import cn.kgc.service.ResumeWorkExpService;
import cn.kgc.utils.PagedGridResult;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author YC
 */
@RestController
@RequestMapping("/resume")
@Api(tags = "简历表模块")
@Validated
@Slf4j
public class ResumeController {

    @Resource
    private ResumeService resumeService;
    @Resource
    private ResumeWorkExpService resumeWorkExpService;
    @Resource
    private ResumeProjectExpService resumeProjectExpService;
    @Resource
    private ResumeTccService resumeTccService;

    @PostMapping("/hrReadResume")
    @ApiOperation(value = "HR 查看求职者投递的简历")
    public GraceJSONResult hrReadResume(@RequestParam("resumeId") String resumeId) {
        return GraceJSONResult.ok(resumeService.queryResumeVo(resumeId));
    }

    @PostMapping("/init")
    @ApiOperation(value = "初始化简历")
    public GraceJSONResult initResume(@RequestParam("userId") String userId) {
        return GraceJSONResult.ok(resumeTccService.tryInitResume(null, userId));
    }

    @PostMapping("/app/modify")
    @ApiOperation(value = "求职者修改简历")
    public GraceJSONResult modifyResume(@RequestBody ResumeUpdateForm form) {
        // 验证当前登录的信息
        String loginUserId = JwtInterceptor.userIds.get();
        if (loginUserId.isEmpty()) {
            GraceException.display(ResponseStatusEnum.UN_LOGIN);
        }
        // 验证当前登录的用户是否是简历的拥有者
        Resume resume = resumeService.getById(form.getId());
        if (!resume.getUserId().equals(loginUserId)) {
            GraceException.display(ResponseStatusEnum.NO_AUTH);
        }
        // 修改简历
        return GraceJSONResult.ok(resumeService.updateResume(form));
    }

    @PostMapping("/app/detail")
    @ApiOperation(value = "求职者查询个人简历")
    @SentinelResource(value = "detail")
    public GraceJSONResult detail(String resumeId) {
        // 验证当前登录的信息
        String loginUserId = JwtInterceptor.userIds.get();
        if (loginUserId.isEmpty()) {
            GraceException.display(ResponseStatusEnum.UN_LOGIN);
        }
        // 验证当前登录的用户是否是简历的拥有者
        Resume resume = resumeService.getById(resumeId);
        if (!resume.getUserId().equals(loginUserId)) {
            GraceException.display(ResponseStatusEnum.NO_AUTH);
        }
        return GraceJSONResult.ok(resume);
    }

    @PostMapping("/app/queryResumeWorkExp")
    @ApiOperation(value = "查看自己简历的工作经验")
    @RequireLogin(
            value = {LoginType.USER_APP_LOGIN}
    )
    public GraceJSONResult queryResumeWorkExp(String resumeId) {
        String loginUserId = JwtInterceptor.userIds.get();
        // 验证当前登录的用户是否是简历的拥有者
        return GraceJSONResult.ok(resumeWorkExpService.queryResumeWorkExpByResumeId(resumeId, loginUserId));
    }

    @PostMapping("/app/addOrModifyWorkExp")
    @ApiOperation(value = "新增/修改自己的工作经验")
    public GraceJSONResult addOrModifyWorkExp(@RequestBody ResumeWorkExpAddOrUpdateForm form) {
        // 验证当前登录的信息
        String loginUserId = JwtInterceptor.userIds.get();
        if (loginUserId.isEmpty()) {
            GraceException.display(ResponseStatusEnum.UN_LOGIN);
        }
        return GraceJSONResult.ok(resumeWorkExpService.addOrModifyResumeWorkExp(form, loginUserId));
    }

    @PostMapping("/app/deleteWorkExp")
    @ApiOperation(value = "删除自己的工作经验")
    public GraceJSONResult deleteWorkExp(@RequestParam("workExpId") String workExpId, @RequestParam("resumeId") String resumeId) {
        // 验证当前登录的信息
        String loginUserId = JwtInterceptor.userIds.get();
        if (loginUserId.isEmpty()) {
            GraceException.display(ResponseStatusEnum.UN_LOGIN);
        }
        return GraceJSONResult.ok(resumeWorkExpService.deleteResumeWorkExp(workExpId, resumeId, loginUserId));
    }

    @PostMapping("/app/queryResumeProjectExp")
    @ApiOperation(value = "查看自己的项目经验")
    public GraceJSONResult queryResumeProjectExp(String id, String resumeId) {
        // 验证当前登录的信息
        String loginUserId = JwtInterceptor.userIds.get();
        if (loginUserId.isEmpty()) {
            GraceException.display(ResponseStatusEnum.UN_LOGIN);
        }
        ResumeProjectExp projectExp = resumeProjectExpService.getOne(
                new LambdaQueryWrapper<ResumeProjectExp>()
                        .eq(ResumeProjectExp::getResumeId, resumeId)
                        .eq(ResumeProjectExp::getId, id)
                        .eq(ResumeProjectExp::getUserId, loginUserId)
        );
        return GraceJSONResult.ok(projectExp);
    }

    // TODO 单表curd未完成

    @PostMapping("/refreshResume")
    @ApiOperation(value = "将简历信息刷新到ES中")
    public GraceJSONResult refreshResume(String resumeId, String userId) {
        resumeService.refreshResume(resumeId, userId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/pageResume")
    @ApiOperation(value = "ES分页查询求职者的简历信息")
    public PagedGridResult pageResume(@RequestBody ResumeSearchForm form) {
        return resumeService.pageResume(form);
    }

    // 模板
//    @PostMapping("/.../...")
//    @ApiOperation(value = "...")
//    public GraceJSONResult xxx() {
//
//    }


}
